import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;


public class ThreadUDP extends Thread {

	private Environment _env;
	 
	public ThreadUDP(Environment env){
		_env = env;		
	}

	@Override
	public void run() {
		int size = _env.get_size();
		long dataSize = 10*Environment.KB64;
		int nbThread = _env.get_nbThread();
		long totalDataSize = dataSize*nbThread;
		long i=0;
		
		int port = _env.get_port();
		DatagramSocket serverSocket = null;
		try {
			serverSocket = new DatagramSocket(port);
		} catch (SocketException e) {
			e.printStackTrace();
		}
		// datas
		byte receiveData[] = new byte[size]; 
		byte sendData[] = new byte[size];
		for (int j=0; j< size; j++){
			sendData[j] = (byte) 51;
		}
		
		// network's information
		DatagramPacket receivePacket;
		DatagramPacket sendPacket; 
		InetAddress IPAddress;
		
		while(i<totalDataSize){
			//System.out.println("dataTransfert, server side");
			receivePacket = new DatagramPacket(receiveData, receiveData.length);
			try {
				serverSocket.receive(receivePacket);
			} catch (IOException e) {
				e.printStackTrace();
			}
			receiveData = receivePacket.getData();
			IPAddress = receivePacket.getAddress();
			port = receivePacket.getPort();
			
			sendPacket = new DatagramPacket(sendData, sendData.length, IPAddress, port);
			try {
				serverSocket.send(sendPacket);
			} catch (IOException e) {
				e.printStackTrace();
			}
			i += size;
		}

	}
	
}
